// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) definits fieldsfirst ansi space safe 
// Source File Name:   World.java

package jones;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import jones.rigid.ConvexPolygon;
import jones.rigid.OverlappingRigidBodyException;
import jones.rigid.RigidBody;
import jones.rigid.RigidWorld;

// Referenced classes of package jones:
//            SimObject, Light

public class World extends Observable
    implements SimObject, Serializable
{

    private RigidWorld rigidWorld = null;
    private List lights = null;
    private List simObjects = null;
    private int latency = 0;
    private boolean isFantasyMode = false;

    public World()
    {
        rigidWorld = new RigidWorld();
        lights = ((List) (new ArrayList()));
        simObjects = ((List) (new ArrayList()));
        latency = 0;
        isFantasyMode = true;
    }

    public synchronized void update(double time)
    {
        rigidWorld.update(time);
        for (int i = 0; i < simObjects.size(); i++)
            ((SimObject)simObjects.get(i)).update(time);

    }

    public synchronized void addRigidBody(RigidBody rigidBody)
        throws OverlappingRigidBodyException
    {
        rigidWorld.add(rigidBody);
    }

    public synchronized void removeRigidBody(RigidBody rigidBody)
    {
        rigidWorld.remove(rigidBody);
    }

    public synchronized void addLight(Light light)
    {
        lights.add(((Object) (light)));
    }

    public synchronized void removeLight(Light light)
    {
        lights.remove(((Object) (light)));
    }

    public synchronized void addSimObject(SimObject simObject)
    {
        simObjects.add(((Object) (simObject)));
    }

    public synchronized void removeSimObject(SimObject simObject)
    {
        simObjects.remove(((Object) (simObject)));
    }

    public synchronized void setLatency(int latency)
    {
        if (latency < 0)
        {
            throw new IllegalArgumentException("World latency cannot be a negative number.");
        } else
        {
            this.latency = latency;
            return;
        }
    }

    public synchronized int getLatency()
    {
        return latency;
    }

    public synchronized void setFantasyMode(boolean isFantasyMode)
    {
        this.isFantasyMode = isFantasyMode;
    }

    public synchronized boolean isFantasyMode()
    {
        return isFantasyMode;
    }

    public synchronized double getBrightness(double x, double y, double direction)
    {
        double value = 0.0D;
        for (int i = 0; i < lights.size(); i++)
        {
            Light light = (Light)lights.get(i);
            double lightX = light.getX();
            double lightY = light.getY();
            double brightness = light.getBrightness();
            double theta = Math.atan2(lightY - y, lightX - x) - direction;
            if (!rigidWorld.intersects(x, y, lightX, lightY) && Math.cos(theta) >= 0.0D)
                value += (Math.cos(theta) * brightness) / (java.awt.geom.Point2D.distance(x, y, lightX, lightY) + 1.0D);
        }

        return value;
    }

    public synchronized boolean intersects(ConvexPolygon p)
    {
        return rigidWorld.intersects(p);
    }

    public synchronized boolean intersects(double x1, double y1, double x2, double y2)
    {
        return rigidWorld.intersects(x1, y1, x2, y2);
    }

    public synchronized boolean isColliding(RigidBody body)
    {
        return rigidWorld.isColliding(body);
    }

    public synchronized boolean createCollisionGraph()
    {
        return rigidWorld.createCollisionGraph();
    }

    public synchronized boolean isPushing(RigidBody body)
    {
        return rigidWorld.isPushing(body);
    }

    public String toString()
    {
        String s = new String();
        s = "rigidWorld = " + rigidWorld + "\n" + "lightCount = " + lights.size() + "\n";
        for (int i = 0; i < lights.size(); i++)
            s = s + lights.get(i) + "\n";

        s = s + "simObjects.size() = " + simObjects.size() + "\n";
        for (int i = 0; i < simObjects.size(); i++)
            s = s + simObjects.get(i) + "\n";

        return s;
    }
}
